package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.AttendClassLecture;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.CourseTemplate;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.mapper.AttendClassLectureMapper;
import com.yuyou.fn.educational.service.IAttendClassLectureService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.ICourseTemplateService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.AttendClassLectureQueryVo;
import com.yuyou.fn.educational.vo.AttendWarningQueryVo;
import com.yuyou.fn.educational.vo.AttendWarningVo;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.ISysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * AttendClassLecture 表数据服务层接口实现类
 *
 */
@Service
public class AttendClassLectureServiceImpl extends SuperServiceImpl<AttendClassLectureMapper, AttendClassLecture,String> implements IAttendClassLectureService {

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private ICourseTemplateService courseTemplateService;

    @Resource
    private ITeacherService teacherService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Override
    public Page<AttendClassLecture> findAttendClassLectureByQuery(AttendClassLectureQueryVo attendClassLectureQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Example<AttendClassLecture> example = AttendClassLecture.createExample();
        Example.Criteria criteria = example.createCriteria();
        String timeSegment = attendClassLectureQueryVo.getTimeSegment();
        attendClassLectureQueryVo.setTimeSegment(null);
        CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds() );
        CriteriaUtils.fillCondition(criteria,attendClassLectureQueryVo);
        criteria.andIn("status", Arrays.asList(1,3));
        if(StringUtils.isNotEmpty(timeSegment)){
            String[] parts  = timeSegment.split("-");
            if(parts.length == 2 ){
                criteria.andEqualTo("startTime", parts[0]);
                criteria.andEqualTo("endTime", parts[1]);
            }
        }
        return findPageByExample(example , pageNo , pageSize );
    }


    @Override
    public Page<AttendWarningVo> findAttendWarningPage(AttendWarningQueryVo attendWarningQueryVo,  String nowTime, int pageNo, int pageSize) {
        List<AttendWarningVo> attendWarningVoList=this.findAttendWarningList(attendWarningQueryVo,nowTime,(pageNo-1)*pageSize,pageSize);
        Page<AttendWarningVo> page=new Page<>(pageNo,pageSize);
        page.setRecords(attendWarningVoList);
        page.setTotal(this.countAttendWarning(attendWarningQueryVo,nowTime));
        return page;
    }

    @Override
    public List<AttendWarningVo> findAttendWarningList(AttendWarningQueryVo attendWarningQueryVo,String nowTime,int begin,int limit) {
        return autoMapper.findAttendWarningList(attendWarningQueryVo,nowTime,begin,limit);
    }

    @Override
    public Integer countAttendWarning(AttendWarningQueryVo attendWarningQueryVo, String nowTime) {
        Integer  total=autoMapper.countAttendWarning(attendWarningQueryVo,nowTime);
        return total;
    }

    @Override
    public SXSSFWorkbook exportAttendWarningByQuery(AttendWarningQueryVo attendWarningQueryVo, String nowTime ) {
        List<AttendWarningVo> attendWarningVoList=this.findAttendWarningList(attendWarningQueryVo,nowTime,0,50000);
        return attendWarning2Excel(attendWarningVoList);
    }

    @Override
    public List<AttendClassLecture> findAttendClassLectureListByQuery(AttendClassLectureQueryVo attendClassLectureQueryVo) {
        Example<AttendClassLecture> example = AttendClassLecture.createExample();
        Example.Criteria criteria = example.createCriteria();
        String timeSegment = attendClassLectureQueryVo.getTimeSegment();
        attendClassLectureQueryVo.setTimeSegment(null);
        CriteriaUtils.fillCondition(criteria,attendClassLectureQueryVo);
        criteria.andIn("status", Arrays.asList(1,3));
        if(StringUtils.isNotEmpty(timeSegment)){
            String[] parts  = timeSegment.split("-");
            if(parts.length == 2 ){
                criteria.andGreaterThanOrEqualTo("startTime", parts[0]);
                criteria.andLessThanOrEqualTo("startTime", parts[1]);
            }
        }
        return findListByExample(example);
    }

    @Override
    public List<String> findClassTimeByPeriodDate(DataPermission dataPermission, Long periodId, Date startClassDate, Date endClassDate) {
        List<String> classTimeByPeriodDates = autoMapper.findClassTimeByPeriodDate(periodId, startClassDate, endClassDate, dataPermission.getBusTeamIds());
        return classTimeByPeriodDates;
    }

    public static SXSSFWorkbook attendWarning2Excel(List<AttendWarningVo> attendWarningVoList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("考勤预警");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"期数","班级编号","班级名称","年级","科目","业务组","讲次",	"上课日期","上课时间","总人数","任课老师"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        for (AttendWarningVo  attendWarningVo:attendWarningVoList){
            int cellNum = 0;
            SXSSFRow row = sheet.createRow(rowNum++);

            SXSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getPeriodName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getClassNo()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getClassName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getGradeName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getSubjectName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getBusTeamName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(attendWarningVo.getLectureNo()==null?"":attendWarningVo.getLectureNo().toString());


            cell = row.createCell(cellNum++);
            cell.setCellValue(attendWarningVo.getClassDate()==null?"": Utils.formatDate(attendWarningVo.getClassDate(),"yyyy-MM-dd"));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getStartTime())+"-"+attendWarningVo.getEndTime());

            cell = row.createCell(cellNum++);
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue(attendWarningVo.getTotalPersonNum());


            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(attendWarningVo.getTeacherName()));


        }
        return excel;
    }

}